package com.czk.service;

import com.czk.dao.CommentRepository;
import com.czk.pojo.Comment;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Pattern;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/7/7 14:39
 */
@Service
public class CommentService {
    @Autowired
    private CommentRepository commentRepository;
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 保存一个评论
     *
     * @param comment
     */
    public void saveComment(Comment comment) {
        //如果需要自定义主键，可以在这里指定主键；如果不指定主键，MongoDB会自动生成主键
        //设置一些默认初始值。。。
        //调用dao
        commentRepository.save(comment);
    }

    /**
     * 更新评论
     *
     * @param comment
     */
    public void updateComment(Comment comment) {
        //调用dao
        commentRepository.save(comment);
    }

    /**
     * 根据id删除评论
     *
     * @param id
     */
    public void deleteCommentById(String id) {
        //调用dao
        commentRepository.deleteById(id);
    }

    /**
     * 查询所有评论
     *
     * @return
     */
    public List<Comment> findCommentList() {
        //调用dao
        return commentRepository.findAll();
    }

    /**
     * 根据id查询评论
     *
     * @param id
     * @return
     */
    public Comment findCommentById(String id) {
        //调用dao
        return commentRepository.findById(id).get();
    }

    public Page<Comment> findCommentListByParentid(String parentid, int page, int size) {
        return commentRepository.findByParentId(parentid, PageRequest.of(page - 1, size));
    }

    /**
     * 点赞数加一
     *
     * @param id
     */
    public void updateCommentLikenum(String id) {

        //  查询条件
        Query query = Query.query(Criteria.where("_id").is(id));
        //  更新条件
        Update update = new Update();
        update.inc("likenum");
        mongoTemplate.updateFirst(query, update, Comment.class);
    }

    /**
     * 分页查询
     */
    public void findPageCommentList() {
        int pageNo = 1;
        int pageSize = 3;
        String name = "陈";
        String regex = String.format("%s%s%s", ".*", name, ".*$");
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Query query = new Query(
                Criteria.where("content").regex(pattern)
        );
        // 分页构建
        // 查询记录数
        long count = mongoTemplate.count(query, Comment.class);
        // 分页
        List<Comment> commentList = mongoTemplate.find(query.skip((pageNo - 1) * pageSize).limit(pageSize), Comment.class);
        System.out.println(count);
        System.out.println(commentList);
    }

    /**
     * 更新操作
     */
    public void updateComment() {
        Comment comment = mongoTemplate.findById("60e54e4e3c3f462250e041b8", Comment.class);

        comment.setState("更新2");
        Query query = new Query(
                Criteria.where("_id").is(comment.getId())
        );
        Update update = new Update();
        update.set("state", comment.getState());
        UpdateResult upsert = mongoTemplate.upsert(query, update, Comment.class);
        long modifiedCount = upsert.getModifiedCount();
        System.out.println("影响的行数" + modifiedCount);

    }

    public void delete() {
        Query query = new Query(
                Criteria.where("_id").is("60e54e4e3c3f462250e041b8")
        );
        DeleteResult remove = mongoTemplate.remove(query, Comment.class);
        System.out.println(remove.getDeletedCount());
    }

    public void findUserList() {
        Comment comment = new Comment();
        comment.setUserid("1");
        Example example = Example.of(comment);
        List all = commentRepository.findAll(example);
        System.out.println(all);
    }

    /**
     * 模糊查询的构建
     */
    public void likeSelect() {
        // 设置模糊查询的匹配规则
        ExampleMatcher matcher = ExampleMatcher.matching();
        matcher.withStringMatcher(
                ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Comment comment = new Comment();
        comment.setUserid("1");
        Example example = Example.of(comment);
        List all = commentRepository.findAll(example);
        System.out.println(all);
    }
}
